<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>nativebuttonrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_nativebuttonrenderer.js.html">nativebuttonrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Native browser button renderer for {@link goog.ui.Button}s.
<a name="line17"></a> *
<a name="line18"></a> * @author attila@google.com (Attila Bodis)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.NativeButtonRenderer&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line24"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line25"></a>goog.require(&#39;goog.ui.ButtonRenderer&#39;);
<a name="line26"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Renderer for {@link goog.ui.Button}s.  Renders and decorates native HTML
<a name="line32"></a> * button elements.  Since native HTML buttons have built-in support for many
<a name="line33"></a> * features, overrides many expensive (and redundant) superclass methods to
<a name="line34"></a> * be no-ops.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.ui.ButtonRenderer}
<a name="line37"></a> */
<a name="line38"></a>goog.ui.NativeButtonRenderer = function() {
<a name="line39"></a>  goog.ui.ButtonRenderer.call(this);
<a name="line40"></a>};
<a name="line41"></a>goog.inherits(goog.ui.NativeButtonRenderer, goog.ui.ButtonRenderer);
<a name="line42"></a>goog.addSingletonGetter(goog.ui.NativeButtonRenderer);
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/** @override */
<a name="line46"></a>goog.ui.NativeButtonRenderer.prototype.getAriaRole = function() {
<a name="line47"></a>  // Native buttons don&#39;t need ARIA roles to be recognized by screen readers.
<a name="line48"></a>  return undefined;
<a name="line49"></a>};
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Returns the button&#39;s contents wrapped in a native HTML button element.  Sets
<a name="line54"></a> * the button&#39;s disabled attribute as needed.
<a name="line55"></a> * @param {goog.ui.Control} button Button to render.
<a name="line56"></a> * @return {Element} Root element for the button (a native HTML button element).
<a name="line57"></a> * @override
<a name="line58"></a> */
<a name="line59"></a>goog.ui.NativeButtonRenderer.prototype.createDom = function(button) {
<a name="line60"></a>  this.setUpNativeButton_(button);
<a name="line61"></a>  return button.getDomHelper().createDom(&#39;button&#39;, {
<a name="line62"></a>    &#39;class&#39;: this.getClassNames(button).join(&#39; &#39;),
<a name="line63"></a>    &#39;disabled&#39;: !button.isEnabled(),
<a name="line64"></a>    &#39;title&#39;: button.getTooltip() || &#39;&#39;,
<a name="line65"></a>    &#39;value&#39;: button.getValue() || &#39;&#39;
<a name="line66"></a>  }, button.getCaption() || &#39;&#39;);
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Overrides {@link goog.ui.ButtonRenderer#canDecorate} by returning true only
<a name="line72"></a> * if the element is an HTML button.
<a name="line73"></a> * @param {Element} element Element to decorate.
<a name="line74"></a> * @return {boolean} Whether the renderer can decorate the element.
<a name="line75"></a> * @override
<a name="line76"></a> */
<a name="line77"></a>goog.ui.NativeButtonRenderer.prototype.canDecorate = function(element) {
<a name="line78"></a>  return element.tagName == &#39;BUTTON&#39; ||
<a name="line79"></a>      (element.tagName == &#39;INPUT&#39; &amp;&amp; (element.type == &#39;button&#39; ||
<a name="line80"></a>          element.type == &#39;submit&#39; || element.type == &#39;reset&#39;));
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/** @override */
<a name="line85"></a>goog.ui.NativeButtonRenderer.prototype.decorate = function(button, element) {
<a name="line86"></a>  this.setUpNativeButton_(button);
<a name="line87"></a>  if (element.disabled) {
<a name="line88"></a>    // Add the marker class for the DISABLED state before letting the superclass
<a name="line89"></a>    // implementation decorate the element, so its state will be correct.
<a name="line90"></a>    goog.dom.classes.add(element,
<a name="line91"></a>        this.getClassForState(goog.ui.Component.State.DISABLED));
<a name="line92"></a>  }
<a name="line93"></a>  return goog.ui.NativeButtonRenderer.superClass_.decorate.call(this, button,
<a name="line94"></a>      element);
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Native buttons natively support BiDi and keyboard focus.
<a name="line100"></a> * @suppress {visibility} getHandler and performActionInternal
<a name="line101"></a> * @override
<a name="line102"></a> */
<a name="line103"></a>goog.ui.NativeButtonRenderer.prototype.initializeDom = function(button) {
<a name="line104"></a>  // WARNING:  This is a hack, and it is only applicable to native buttons,
<a name="line105"></a>  // which are special because they do natively what most goog.ui.Controls
<a name="line106"></a>  // do programmatically.  Do not use your renderer&#39;s initializeDom method
<a name="line107"></a>  // to hook up event handlers!
<a name="line108"></a>  button.getHandler().listen(button.getElement(), goog.events.EventType.CLICK,
<a name="line109"></a>      button.performActionInternal);
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * @override
<a name="line115"></a> * Native buttons don&#39;t support text selection.
<a name="line116"></a> */
<a name="line117"></a>goog.ui.NativeButtonRenderer.prototype.setAllowTextSelection =
<a name="line118"></a>    goog.nullFunction;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * @override
<a name="line123"></a> * Native buttons natively support right-to-left rendering.
<a name="line124"></a> */
<a name="line125"></a>goog.ui.NativeButtonRenderer.prototype.setRightToLeft = goog.nullFunction;
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * @override
<a name="line130"></a> * Native buttons are always focusable as long as they are enabled.
<a name="line131"></a> */
<a name="line132"></a>goog.ui.NativeButtonRenderer.prototype.isFocusable = function(button) {
<a name="line133"></a>  return button.isEnabled();
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * @override
<a name="line139"></a> * Native buttons natively support keyboard focus.
<a name="line140"></a> */
<a name="line141"></a>goog.ui.NativeButtonRenderer.prototype.setFocusable = goog.nullFunction;
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * @override
<a name="line146"></a> * Native buttons also expose the DISABLED state in the HTML button&#39;s
<a name="line147"></a> * {@code disabled} attribute.
<a name="line148"></a> */
<a name="line149"></a>goog.ui.NativeButtonRenderer.prototype.setState = function(button, state,
<a name="line150"></a>    enable) {
<a name="line151"></a>  goog.ui.NativeButtonRenderer.superClass_.setState.call(this, button, state,
<a name="line152"></a>      enable);
<a name="line153"></a>  var element = button.getElement();
<a name="line154"></a>  if (element &amp;&amp; state == goog.ui.Component.State.DISABLED) {
<a name="line155"></a>    element.disabled = enable;
<a name="line156"></a>  }
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * @override
<a name="line162"></a> * Native buttons store their value in the HTML button&#39;s {@code value}
<a name="line163"></a> * attribute.
<a name="line164"></a> */
<a name="line165"></a>goog.ui.NativeButtonRenderer.prototype.getValue = function(element) {
<a name="line166"></a>  // TODO(attila): Make this work on IE!  This never worked...
<a name="line167"></a>  // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
<a name="line168"></a>  // for a description of the problem.
<a name="line169"></a>  return element.value;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * @override
<a name="line175"></a> * Native buttons also expose their value in the HTML button&#39;s {@code value}
<a name="line176"></a> * attribute.
<a name="line177"></a> */
<a name="line178"></a>goog.ui.NativeButtonRenderer.prototype.setValue = function(element, value) {
<a name="line179"></a>  if (element) {
<a name="line180"></a>    // TODO(attila): Make this work on IE!  This never worked...
<a name="line181"></a>    // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
<a name="line182"></a>    // for a description of the problem.
<a name="line183"></a>    element.value = value;
<a name="line184"></a>  }
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * @override
<a name="line190"></a> * Native buttons don&#39;t need ARIA states to support accessibility, so this is
<a name="line191"></a> * a no-op.
<a name="line192"></a> */
<a name="line193"></a>goog.ui.NativeButtonRenderer.prototype.updateAriaState = goog.nullFunction;
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Sets up the button control such that it doesn&#39;t waste time adding
<a name="line198"></a> * functionality that is already natively supported by native browser
<a name="line199"></a> * buttons.
<a name="line200"></a> * @param {goog.ui.Control} button Button control to configure.
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.ui.NativeButtonRenderer.prototype.setUpNativeButton_ = function(button) {
<a name="line204"></a>  button.setHandleMouseEvents(false);
<a name="line205"></a>  button.setAutoStates(goog.ui.Component.State.ALL, false);
<a name="line206"></a>  button.setSupportedState(goog.ui.Component.State.FOCUSED, false);
<a name="line207"></a>};
</pre>


</body>
</html>
